package mergeArrays;

import java.util.ArrayList;
import java.util.List;

class Solution {
    public int[][] mergeArrays(int[][] nums1, int[][] nums2) {
        int i = 0;
        int j = 0;
        int n = nums1.length;
        int m = nums2.length;
        List<int[]> ans = new ArrayList<>();
        while (i<n||j<m) {
            int[] t = new int[2];
            if(j>=m) {
                t = nums1[i];
                i++;
            }else if(i>=n) {
                t = nums2[j];
                j++;
            }else if(nums1[i][0] == nums2[j][0]) {
                t[0] = nums1[i][0];
                t[1] = nums1[i][1] + nums2[j][1];
                i++;
                j++;
            }else if(nums1[i][0] < nums2[j][0]) {
                t = nums1[i];
                i++;
            }else if(nums1[i][0] > nums2[j][0]) {
                t = nums2[j];
                j++;
            }
            ans.add(t);
        }
        return ans.toArray(new int[ans.size()][]);
    }
}
